/**
 * @CopyRight weitaming.com
 * @author wuyahui
 * @E-mail wuyahui@ispied.com
 * @version 1.0.0
 * @Date  2014年5月6日 上午11:12:52
 */
package com.tstudy.laoshibang.manager;

import java.util.ArrayList;
import java.util.List;

import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.db.DBManagerImpl;
import com.tstudy.laoshibang.mode.City;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;

import android.text.TextUtils;

public class CityManager extends DBManagerImpl {
	private CityManager() {
	}

	public static CityManager instance = new CityManager(City.class);

	private CityManager(Class<City> clazz) {
		super(clazz);
	}

	public static CityManager getInstance() {
		return instance;
	}

	/**
	 * 初始化缓存城市列表
	 */
	public void initCityData() {
		int count = getCount();
		if (count > 0) {
			return;
		}
		long beginTime = System.currentTimeMillis();
		List<String> citylist = CommonUtil.readFileToList(
				BaseApplication.mContext, "citylist");
		ArrayList<City> cities = new ArrayList<City>();
		for (String str : citylist) {
			if (!TextUtils.isEmpty(str)) {
				String[] cityArray = str.split(",");
				String type = cityArray[0];
				int cityId = Integer.parseInt(cityArray[1]);
				int parentProvinceId = 0;
				int parentCityId = 0;
				String cityNameStr = cityArray[2];
				if (type.equals("city")) {
					parentProvinceId = (cityId / 10000) * 10000;
				} else if (type.equals("county")) {
					parentProvinceId = (cityId / 10000) * 10000;
					parentCityId = (cityId / 100) * 100;
				}
				City city = new City(type, cityId, cityNameStr,
						parentProvinceId, parentCityId);
				cities.add(city);
			}
		}
		insertOrUpdate(cities);

		long executeTime = System.currentTimeMillis() - beginTime;
		LogUtil.d("--initCityDatatime ==" + executeTime);
	}

}
